package com.liuyucheng.generic.wildcard;

import java.util.Comparator;
import java.util.TreeSet;

public class TestWildCardLimit {
    public static void main(String[] args) {
        /*
         * TreeSet这里的构造方法用到了泛型通配符下限
         * 只允许使用Cat类型以及父类类型的比较器
         * 这样能保证比较方法中的属性都是存在的
         */
        TreeSet<Cat> treeSet = new TreeSet<>(new Comparator2());
//        TreeSet<Cat> treeSet = new TreeSet<>(new Comparator1());
//        TreeSet<Cat> treeSet = new TreeSet<>(new Comparator3());
        treeSet.add(new Cat("jerry",20));
        treeSet.add(new Cat("amy",22));
        treeSet.add(new Cat("frank",35));
        treeSet.add(new Cat("jim",15));
        treeSet.forEach((item)->{
            System.out.println("item = " + item);
        });
    }
}

class Comparator1 implements Comparator<Animal>{
    @Override
    public int compare(Animal o1, Animal o2) {
        return o1.name.compareTo(o2.name);
    }
}
class Comparator2 implements Comparator<Cat>{
    @Override
    public int compare(Cat o1, Cat o2) {
        return o1.age - o2.age;
    }
}
class Comparator3 implements Comparator<MiniCat>{
    @Override
    public int compare(MiniCat o1, MiniCat o2) {
        return o1.level - o2.level;
    }
}